package com.yc.damai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yc.damai.entity.Cart;
import com.yc.damai.entity.Orders;
import com.yc.damai.mapper.CartMapper;
import com.yc.damai.vo.Result;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ly
 * @since 2022-11-01
 */
@RestController
@RequestMapping("/damai/cart")
public class CartController {

    @Resource
    private CartMapper mapper;

    @RequestMapping("queryAll")
    List<Cart> queryAll(int uid) {
//        LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
//        lqw.eq(Cart::getUid, uid);
//        return mapper.selectList(lqw);
        return mapper.selectByUid(uid);
    }

    @Transactional
    @RequestMapping("clearCart")
    Result clearCart(@RequestBody Orders orders){
        orders.getOrderitems().forEach(item->{
            LambdaQueryWrapper<Cart> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Cart::getUid, orders.getUid());
            lqw.eq(Cart::getPid, item.getPid());
            mapper.delete(lqw);
        });
        return new Result(1, "用户购物清空成功", null);
    }


}

